package com.local.admin.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.order.feign.OrderServiceFeign;
import com.local.order.pojo.Order;
import com.local.order.pojo.OrderListRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private OrderServiceFeign orderServiceFeign;



    /**
     * 订单管理列表页
     * @param request
     * @return
     */
    @GetMapping
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "orders");

        return "admin/order";
    }

    /**
     * 查询订单列表，支持翻页
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam("sidx") String sort, @RequestParam("order") String order) {

        page = Objects.isNull(page) || page < 1 ? 1 : page;
        limit = Objects.isNull(limit) || limit < 1 ? 10 : limit;
        sort = sort.isEmpty() ? "order_id" : sort;
        order = order.isEmpty() ? "desc" : order;
        OrderListRequestParam requestParam = new OrderListRequestParam(page, limit, sort, order, -1);
        ResultUtil<PageResultUtil<Order>> listResult = orderServiceFeign.orderList(requestParam);
        return listResult;
    }

    /**
     * 修改
     * @param order
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Order order) {
        String updateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        order.setUpdateTime(updateTime);
        ResultUtil updateResult = orderServiceFeign.orderUpdate(order);
        return updateResult;
    }


    /**
     * 批量修改订单状态为：配货完成
     * @param ids
     * @return
     */
    @PostMapping("/checkDone")
    @ResponseBody
    public ResultUtil checkDone(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }
        ResultUtil result = orderServiceFeign.orderCheckDone(ids);
        return result;
    }


    /**
     * 批量修改订单状态为：出库完成
     * @param ids
     * @return
     */
    @PostMapping("/checkOut")
    @ResponseBody
    public ResultUtil checkOut(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }
        ResultUtil result = orderServiceFeign.orderCheckOut(ids);
        return result;
    }


    /**
     * 批量修改订单状态为：商家关闭
     * @param ids
     * @return
     */
    @PostMapping("/close")
    @ResponseBody
    public ResultUtil close(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return new ResultUtil(201, "参数错误");
        }
        ResultUtil result = orderServiceFeign.orderClose(ids);
        return result;
    }


    /**
     * 查询订单的所有商品明细
     * @param orderId
     * @return
     */
    @GetMapping("/items/{orderId}")
    @ResponseBody
    public ResultUtil items(@PathVariable("orderId") Integer orderId) {
        if (orderId <= 0) {
            return new ResultUtil(201, "参数错误");
        }

        ResultUtil result = orderServiceFeign.orderItems(orderId);
        return result;
    }
}
